define(['jquery', 'bootstrap', 'backend', 'table', 'form','cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'fas/zctype/index' + location.search,
                    add_url: 'fas/zctype/add',
                    edit_url: 'fas/zctype/edit',
                    del_url: 'fas/zctype/del',
                    multi_url: 'fas/zctype/multi',
                    import_url: 'fas/zctype/import',
                    table: 'fas_zctype',
                }
            });

            var table = $("#table");
            var buttons = [];
            buttons.push({
                name: 'op1',
                title: __('耗材BOM'),
                classname: 'btn btn-xs btn-info btn-addtabs',
                icon: 'fa fa-pied-piper',
                url: 'fas/zctypebom/index'
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        //{checkbox: true},
                        {
                            field: 'operate',
                            width: "130px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: buttons,
                            formatter: Table.api.formatter.operate
                        },
                        //{field: 'id', title: __('Id'), operate:false},
                        {field: 'name', title: __('Name'), operate: 'LIKE %...%', placeholder:__('模糊搜索，*表示任意字符')},
                        {field: 'xh', title: __('Xh'), operate: 'LIKE %...%', placeholder:__('模糊搜索，*表示任意字符')},
                        {field: 'type', title: __('Type'), searchList: {"成型设备":__('成型设备'),"线外加工设备":__('线外加工设备'),"组底设备":__('组底设备'),"裁断设备":__('裁断设备'),"针车设备":__('针车设备'),"其他设备":__('其他设备')}, formatter: Table.api.formatter.normal},
                        {field: 'zclh', title: __('Zclh')},
                        //{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                    var myColumns=[];
                    $.each(table.bootstrapTable('getHiddenColumns'), function(i, item) {
                         myColumns.push(item.field);
                    });
                    $.cookie('fa_fas_zctype', JSON.stringify(myColumns), { expires: 365, path: '/'});
             });
             if($.cookie('fa_fas_zctype')){
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_fas_zctype')), function(i, item) {
                  table.bootstrapTable('hideColumn', item);
                });
             }

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});